import { layerKeys } from "@/common/symbolKeys"

/**
 * @type
 */

/**
 * 图层组合式函数，副作用为给map添加指定图层，同时挂载到全局对象window上
 * @param {string} id
 * @param {DE.Viewer} viewer
 * @param {DE.LayerType} type
 * @param {string} url
 * @param {JSON} options
 * @returns {{layer: DE.Layer}}
 */
export function useLayer(id, options = {}) {
  let layer
  const { viewer = window?.$dmap, type = DE.LayerType.VECTOR, url = "" } = options
  options.viewer && delete options.viewer
  options.type && delete options.type
  options.url && delete options.url
  switch (type) {
    case DE.LayerType.VECTOR:
      layer = new DE.VectorLayer(id, { clickClose: true })
      break
    case DE.LayerType.PRIMITIVE:
      layer = new DE.PrimitiveLayer(id)
      break
    case DE.LayerType.TILESET:
      layer = new DE.TilesetLayer(id)
      break
    case DE.LayerType.HTML:
      layer = new DE.HtmlLayer(id)
      break
    case DE.LayerType.Dynamic:
      layer = new DE.DynamicLayer(id)
      break
    case DE.LayerType.CLUSTER:
      layer = new DE.ClusterLayer(id)
      break
    case DE.LayerType.CZML:
      layer = new DE.CzmlLayer(id, url, options)
      break
    case DE.LayerType.GEOJSON:
      layer = new DE.GeoJsonLayer(id, url, options)
      break
    case DE.LayerType.KML:
      layer = new DE.KmlLayer(id, url, options)
      break
  }
  // 添加图层
  viewer && layer && viewer.addLayer(layer)
  // 全局注册到windows对象中
  mountedGlobalLayer(id, layer)
  return layer
}

/**
 * 全局挂载图层概念
 * @param k 图层的Key
 * @param layer 待绑定图层
 */
export function mountedGlobalLayer(k, layer) {
  if (window) {
    window[k] = layer
    if (!window[layerKeys.LAYERS]) {
      window[layerKeys.LAYERS] = {}
    }
    // 统一管理所有的图层
    window[layerKeys.LAYERS][k] = layer
    setLayer({ id: k, layer })
  }
}

const setLayer = (info) => window?.$store?.commit("viewer/setLayers", info)
const removeAllLayers = () => window?.$store?.commit("viewer/removeAllLayers")

export function clearLayers(viewer = null) {
  Object.keys(window[layerKeys.LAYERS]).forEach((k) => {
    if (window[k]?.type === DE.LayerType.CZML) {
      viewer && viewer.removeLayer(window[k])
      window[k] = undefined
      delete window[k]
    } else {
      window[k]?.clear()
    }
  })
}

export function layersHasOverlay() {
  let res = false
  Object.keys(window[layerKeys.LAYERS]).forEach((k) => {
    const layer = window[k]
    if (layer) {
      if (layer.type === DE.LayerType.VECTOR) {
        if (layer?.entities?.values?.length) {
          res = true
        }
      } else if (layer.type === DE.LayerType.PRIMITIVE) {
        if (layer?.delegate?.length) {
          res = true
        }
      } else if (layer.type === DE.LayerType.CZML) {
        if (layer.delegate.then) {
          layer.delegate.then((dataSource) => {
            if (dataSource?.entities?.value?.length) {
              res = true
            }
          })
        } else {
          if (layer.delegate?.entities?.value?.length) {
            res = true
          }
        }
      }
    }
  })
  return res
}

